package com.dzsw.redis.service.impl;

import com.dzsw.redis.bean.User;
import com.dzsw.redis.service.IUserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@CacheConfig(cacheNames = "CacheConfigName")
public class UserServiceImpl implements IUserService {

    @Override
    @Cacheable(value = "c_1", key = "'keyName1'")
    public User findUser() {
        System.out.println("执行方法...");
        return new User(1L, "张三", "深圳", "23", new Date());
    }

    @Override
    @CachePut(value = "c_1", key = "'keyName1'")
    public User updateUser() {
        System.out.println("更新用户...");
        return new User(2L, "李四", "北京", "25", new Date());
    }

    @Override
    @CacheEvict(key = "keyName1", allEntries = true)
    public void clearUser() {
        System.out.println("清除缓存...");
    }
}
